﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GHRMS.APP.Classes;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PS_Contacts : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private IContactBLL BLL = new ContactBLL();
        private IMasProvinceBLL proBLL = new MasProvinceBLL();
        private IMasDistrictBLL disBLL = new MasDistrictBLL();
        private IMasNamePrefixBLL npBLL = new MasNamePrefixBLL();
        private IMasFamilyHoodBLL fhBLL = new MasFamilyHoodBLL();
		private Main main = new Main();
        private const int ItemsPerRequest = 10;
        private object _dataItem = null;

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            lblEmpSeq.Text = Session["emp_seq"].ToString();

            if (!IsUserControlPostBack)
            {
                ViewState.Add("IsUserControlPostBack", true);

                if (DataItem != null)
                {
                    if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                    {
                        cmbFH.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "FAM_NAME").ToString(), DataBinder.Eval(DataItem, "FAM_SEQ").ToString()));
                        cmbFH.SelectedValue = DataBinder.Eval(DataItem, "FAM_SEQ").ToString();
                        cmbNamePrefixes.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "PREFIX_NAME").ToString(), DataBinder.Eval(DataItem, "NAME_PREFIX_SEQ").ToString()));
                        cmbNamePrefixes.SelectedValue = DataBinder.Eval(DataItem, "NAME_PREFIX_SEQ").ToString();
                        txtName.Text = DataBinder.Eval(DataItem, "CONTACT_NAME").ToString();
                        txtLastName.Text = DataBinder.Eval(DataItem, "CONTACT_LAST_NAME").ToString();
                        txtCareer.Text = DataBinder.Eval(DataItem, "CONTACT_CAREER").ToString();
                        txtAddress.Text = DataBinder.Eval(DataItem, "CONTACT_ADDRESS").ToString();
                        txtPostalCode.Text = DataBinder.Eval(DataItem, "CONTACT_POSTAL_CODE").ToString();

                        cmbProvince.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "PROV_NAME").ToString(), DataBinder.Eval(DataItem, "PROV_SEQ").ToString()));
                        cmbProvince.SelectedValue = DataBinder.Eval(DataItem, "PROV_SEQ").ToString();

                        cmbDistrict.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "DISTRICT_NAME").ToString(), DataBinder.Eval(DataItem, "DISTRICT_SEQ").ToString()));
                        cmbDistrict.SelectedValue = DataBinder.Eval(DataItem, "DISTRICT_SEQ").ToString();

                        txtTel.Text = DataBinder.Eval(DataItem, "TEL").ToString();
                        txtMobile.Text = DataBinder.Eval(DataItem, "MOBILE").ToString();
                        txtEmail.Text = DataBinder.Eval(DataItem, "EMAIL").ToString();
                        txtCitizen.Text = DataBinder.Eval(DataItem, "CITIZEN").ToString();
                    }
                }
            }
        }
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        protected void LoadProvince()
        {
           // IList<MasProvinceInfo> infoList = proBLL.ListMasProvinces();
            DataSet ds;
            DataTable dt;
            ds = proBLL.LoadMasProvinces((int?)null, (int?)null);
            dt = ds.Tables[0];

            cmbProvince.DataSource = dt;
            cmbProvince.DataBind();
        }
        protected void LoadDistrict(string Amphur, string Province)
        {
            IList<MasDistrictInfo> infoList = disBLL.ListDistricts("", "", Province);
            IList<MasDistrictInfo> List = new List<MasDistrictInfo>();

            for (int i = 0; i < infoList.Count; i++)
            {
                MasDistrictInfo c = new MasDistrictInfo();
                c.Sequence = infoList[i].Sequence;
                c.Name = "ตำบล" + infoList[i].Name + " อำเภอ" + infoList[i].Amphurs.Name;

                List.Add(c);
            }
            cmbDistrict.DataSource = List;
            cmbDistrict.DataBind();
        }
        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            BLL.Insert(lblEmpSeq.Text.Trim()
                , cmbNamePrefixes.SelectedValue
                , txtName.Text.Trim()
                , txtLastName.Text.Trim()
                , cmbFH.SelectedValue
                , txtCareer.Text.Trim()
                , txtAddress.Text.Trim()
                , cmbDistrict.SelectedValue
                , txtPostalCode.Text.Trim()
                , txtTel.Text.Trim()
                , txtMobile.Text.Trim()
                , txtEmail.Text.Trim()
                , txtCitizen.Text.Trim()
				, ssMng.EmpSeqI);
			main.MessageAlert(Page, "บันทึกข้อมูลแล้ว");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            BLL.Update(Int32.Parse(lblSequence.Text.Trim())
                , lblEmpSeq.Text.Trim()
                , cmbNamePrefixes.SelectedValue
                , txtName.Text.Trim()
                , txtLastName.Text.Trim()
                , cmbFH.SelectedValue
                , txtCareer.Text.Trim()
                , txtAddress.Text.Trim()
                , cmbDistrict.SelectedValue
                , txtPostalCode.Text.Trim()
                , txtTel.Text.Trim()
                , txtMobile.Text.Trim()
                , txtEmail.Text.Trim()
                , txtCitizen.Text.Trim()
				, ssMng.EmpSeqI);
			main.MessageAlert(Page, "บันทึกข้อมูลแล้ว");
        }

        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }
        protected void cmbDistrict_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbDistrict.Items.Clear();
            Nullable<int> ProvinceSeq = string.IsNullOrEmpty(lblProvSequence.Text) ? (int?)null : Int32.Parse(lblProvSequence.Text);

            DataSet ds = disBLL.LoadDistricts((int?)null, (int?)null, ProvinceSeq, e.Text);
            DataTable dt = ds.Tables[0];

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbDistrict.Items.Add(new RadComboBoxItem(dt.Rows[i]["DISTRICT_NAME"].ToString(), dt.Rows[i]["DISTRICT_SEQ"].ToString()));

                TableRow tr = new TableRow();

                TableCell tdPosName = new TableCell();
                Label lblPosName = new Label();
                lblPosName.Text = dt.Rows[i]["DISTRICT_NAME"].ToString();
                tdPosName.Width = Unit.Pixel(150);
                tdPosName.Controls.Add(lblPosName);
                tr.Cells.Add(tdPosName);

                TableCell tdClassSpecName = new TableCell();
                Label lblClassSpecName = new Label();
                lblClassSpecName.Text = dt.Rows[i]["AMP_NAME"].ToString();
                tdClassSpecName.Width = Unit.Pixel(150);
                tdClassSpecName.Controls.Add(lblClassSpecName);
                tr.Cells.Add(tdClassSpecName);

                ((Table)cmbDistrict.Items[j].FindControl("tableDistrict")).Controls.Add(tr);
                j++;
            }
            e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        }

        protected void cmbProvince_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbProvince.Items.Clear();

            DataSet ds = null;
            DataTable dt = null;

            ds = proBLL.LoadMasProvinces((int?)null, (int?)null);

            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);

            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbProvince.Items.Add(new RadComboBoxItem(dt.Rows[i]["PROV_NAME"].ToString(), dt.Rows[i]["PROV_SEQ"].ToString()));
            }
        }

        protected void cmbNamePrefixes_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbNamePrefixes.Items.Clear();

            DataSet ds = null;
            DataTable dt = null;

            ds = npBLL.LoadNamePrefixes((int?)null, (int?)null);

            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbNamePrefixes.Items.Add(new RadComboBoxItem(dt.Rows[i]["PREFIX_NAME"].ToString(), dt.Rows[i]["NAME_PREFIX_SEQ"].ToString()));
            }
        }

        protected void cmbFH_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbFH.Items.Clear();

            DataSet ds = null;
            DataTable dt = null;

            ds = fhBLL.LoadFamilyHoods((int?)null);

            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);

            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbFH.Items.Add(new RadComboBoxItem(dt.Rows[i]["FAM_NAME"].ToString(), dt.Rows[i]["FAM_SEQ"].ToString()));
            }
        }

        protected void cmbProvince_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            lblProvSequence.Text = cmbProvince.SelectedValue;
        }
        
    }
}